package cn.jinjimi.app.main.extend;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.main.uicontroller.NavTitleBarController;

/**
 * @author zhenhua
 */
public abstract class BaseNetControlFragment extends BaseFragment implements View.OnClickListener {

    private FrameLayout layoutRoot;
    private RelativeLayout layoutControl;
    private LinearLayout layoutRetry;
    private ProgressBar progressBar;
    private RelativeLayout layoutFail;
    private TextView tvState;
    private RelativeLayout rlBottom;
    protected View bizView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (bizView == null) {
            bizView = inflater.inflate(getBizLayoutResourceId(), null);
            if (controller == null) {
                controller = new NavTitleBarController(bizView.findViewById(R.id.nav_bar));
            }
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initialBiz();
        setBizView();
    }

    /**
     *  加载BizLayout xml
     *  @return res id
     */
    protected abstract int getBizLayoutResourceId();

    /**
     *  初始化Biz数据
     */
    protected abstract void initialBiz();

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_net_control;
    }

    @Override
    protected void initial() {
        initView();
        initListener();
    }

    private void initView() {
        layoutRoot = (FrameLayout) view.findViewById(R.id.rl_root);
        layoutRetry = (LinearLayout) view.findViewById(R.id.ll_error_retry);
        layoutControl = (RelativeLayout) view.findViewById(R.id.rl_control);
        progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
        layoutFail = (RelativeLayout) view.findViewById(R.id.rl_load_fail);
        tvState = (TextView) view.findViewById(R.id.tv_state);
        rlBottom = (RelativeLayout) view.findViewById(R.id.rl_back_bottom);
    }

    private void initListener() {
        view.findViewById(R.id.btn_refresh).setOnClickListener(this);
        view.findViewById(R.id.img_bottom_back).setOnClickListener(this);
        view.findViewById(R.id.btn_retry).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_refresh:
            case R.id.btn_retry:
                netLoadingData();
                break;
            case R.id.img_bottom_back:
                onBackPressed();
                break;
            default:break;
        }
    }

    protected void setBizView() {
        if (bizView != null) {
            if (layoutRoot.getChildCount() <= 1) {
                ViewGroup p = (ViewGroup) bizView.getParent();
                if (p != null) {
                    p.removeView(bizView);
                }
                layoutRoot.addView(bizView, 0);
                netLoadingData();
            }
        }
    }

    @Override
    public View findView() {
        if (null != bizView) {
            return bizView;
        }
        return null;
    }

    /**
     *  显示底部返回按键
     */
    protected void showBottomBack() {
        rlBottom.setVisibility(View.VISIBLE);
    }

    /**
     * 接口返回失败，显示ErrorView
     */
    protected void netLoadError() {
        layoutControl.setVisibility(View.VISIBLE);
        layoutRetry.setVisibility(View.VISIBLE);
        layoutFail.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
    }

    /**
     * 实现网络请求接口
     */
    protected abstract void loadData();

    /**
     * 网络加载中，显示加载View
     */
    protected void netLoadingData() {
        layoutControl.setVisibility(View.VISIBLE);
        layoutFail.setVisibility(View.GONE);
        layoutRetry.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
        loadData();
    }

    /**
     * 网络加载成功，显示BusisnessView
     */
    protected void netLoadSucc() {
        layoutControl.setVisibility(View.GONE);
    }

    protected boolean hadLoadSucc() {
        return layoutControl.getVisibility() == View.GONE;
    }

    /**
     * 有数据的情况下刷新失败，显示BusisnessView
     */
    protected void netLoadFail() {
        layoutControl.setVisibility(View.GONE);
    }

    /**
     * 加载完全空白的View
     *
     * @param message toast语句
     */
    protected void netLoadFaiEmpty(String message) {
        layoutControl.setVisibility(View.VISIBLE);
        layoutFail.setVisibility(View.VISIBLE);
        layoutRetry.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        tvState.setText(getString(R.string.loading_fail_for_show));
        if (!TextUtils.isEmpty(message))
            ToastUtils.toastMessage(getActivity(), message);
    }

    protected void netLoadFaiEmpty() {
        netLoadFaiEmpty(null);
    }

    /**
     * 返回数据成功但是数据为空
     */
    protected void netLoadEmpty() {
        layoutControl.setVisibility(View.VISIBLE);
        layoutFail.setVisibility(View.VISIBLE);
        layoutRetry.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        tvState.setText(getString(R.string.loading_suc_empty));
    }

    public void onBackPressed() {

    }

    public boolean isLoadFailEmpty() {
        return layoutFail != null && layoutFail.getVisibility() == View.VISIBLE;
    }

    protected void setRefreshButtonDoubleMarginBottom() {
        View v = view.findViewById(R.id.btn_refresh);
        if (v != null) {
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            lp.bottomMargin = 2 * lp.bottomMargin;
            v.setLayoutParams(lp);
        }
    }
}